
//
//  WalletViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/21.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "WalletViewController.h"
#import "WalletListViewController.h"
#import "WithdrawDepositViewController.h"
#import "TopUpViewController.h"
#import "WalletHeaderView.h"
#import "UserInfoModel.h"

#define headerViewTag  787878
@interface WalletViewController ()

@end

@implementation WalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationItemTitle: @"钱包"];
    [self setupIndicator];
    [self setupHeaderView];
    [self updateUserInfo];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUserInfo) name:@"UPATE_WALLET_NOTIFI" object:nil];
}
- (void)setupHeaderView{
    WalletHeaderView * headerView  = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([WalletHeaderView class]) owner:nil options:nil].firstObject;
    headerView.btnBlock = ^(WalletTouchType type) {
        if (type == WalletTouchType_topup) {
            [self gotoTopupVC];
        }else if (type == WalletTouchType_withdrawal){
            [self gotoWithdrawDepositVC];
        }
    };
    headerView.tag = headerViewTag;
    [self.view addSubview:headerView];
    [self.view sendSubviewToBack:headerView];
}

- (void)setupIndicator{
    
    self.buttonBarView.shouldCellsFillAvailableWidth = YES;
    self.buttonBarView.backgroundColor = [UIColor whiteColor];
    self.isProgressiveIndicator = YES;
    self.isElasticIndicatorLimit = YES;
    //    self.buttonBarView.leftRightMargin = 10;
    //    self.buttonBarView.scrollsToTop = NO;
    //    self.buttonBarView.bounces = NO;
    // Do any additional setup after loading the view.
    
    self.buttonBarView.selectedBarAlignment = XLSelectedBarAlignmentCenter;
    self.buttonBarView.selectedBar.backgroundColor =  UIColorFromRGB(0xAD9B6A);
    self.buttonBarView.isAutoIndicatorWidth = NO;
    self.buttonBarView.indicatorWidth = 20;
    
    self.buttonBarView.backgroundColor = [UIColor whiteColor];
    self.bottomLineView.backgroundColor =  UIColorFromRGB(0xECEEEE);
    self.buttonBarView.bottomLineHeight = 1;
    
}
-(NSArray *)childViewControllersForPagerTabStripViewController:(XLPagerTabStripViewController *)pagerTabStripViewController{
    
    WalletListViewController * oneVC = [[WalletListViewController alloc]initWithStyle:TableViewControllerStyle_income];
    WalletListViewController * twoVC = [[WalletListViewController alloc]initWithStyle:TableViewControllerStyle_spending];
 
    NSArray * childViewControllers = [NSMutableArray arrayWithObjects:oneVC,twoVC,nil];
    
    return childViewControllers;
}
- (void)gotoWithdrawDepositVC{
    
    WithdrawDepositViewController * tempVC = [[WithdrawDepositViewController alloc]init];
    [self.navigationController pushViewController:tempVC animated:YES];
    
}

- (void)gotoTopupVC{
    TopUpViewController * tempVC =[[TopUpViewController alloc]init];
    [self.navigationController pushViewController:tempVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIColor *)getTabTitleColorSelected{
    
    return UIColorFromRGB(0xAD9B6A);
}
- (UIColor *)getTabTitleColorNor{
    return  UIColorFromRGB(0x333333);
}
- (CGRect )buttonBarViewFrame{
    CGRect  buttonBarViewFrame = CGRectMake(0,222, self.view.frame.size.width, 44);
    return buttonBarViewFrame;
}


- (void)updateUserInfo{
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"User/getUserinfo" params:@{@"token":UserInfo.userToken} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        NSLog(@"%@===",responseObject);
        if (responseObject) {
            NSDictionary * userinfo  = responseObject[@"data"][@"userinfo"];
            [strongSelf saveUserInfo:userinfo];
        }
        WalletHeaderView * headerV = [strongSelf.view viewWithTag:headerViewTag];
        
        [headerV setupWalletMoney:UserInfo.wallet_balance];
    }];
}
- (void)saveUserInfo:(NSDictionary *)userinfo{
    
    UserInfo.avatar_url = [self filterEmpty:userinfo[@"avatar"]];
    UserInfo.userToken =   [self filterEmpty:userinfo[@"token"]];
    UserInfo.userId = [self filterEmpty:userinfo[@"user_id"]];
    UserInfo.nickname = [self filterEmpty:userinfo[@"nickname"]];
    UserInfo.mobile = [self filterEmpty:userinfo[@"mobile"]];
    UserInfo.gender =  [self filterEmpty:userinfo[@"gender"]];
    UserInfo.birthday = [self filterEmpty:userinfo[@"birthday"]];
    UserInfo.redpack_num = [self filterEmpty:userinfo[@"redpack_num"]];
    UserInfo.coupon_num =  [self filterEmpty:userinfo[@"coupon_num"]];
    UserInfo.wallet_balance = [self filterEmpty:userinfo[@"wallet_balance"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"USER_LOGIN_SUCCESSFUL" object:nil];
    
}
- (NSString *)filterEmpty:(id)parameter{
    NSString * str = @"";
    if (parameter &&![parameter isEqual:[NSNull null]] ) {
        str = parameter;
    }else{
        str = @"";
    }
    return str;
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UPATE_WALLET_NOTIFI" object:nil ];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
